package com.zengguang.testkaoshi.controller;

import com.zengguang.testkaoshi.pojo.Testanswer;
import com.zengguang.testkaoshi.services.impl.TestAnswerService;
import com.zengguang.testkaoshi.tool.ResponseMessage;
import com.zengguang.testkaoshi.tool.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "testanswer")
@RestController
@CrossOrigin
public class TestAnswerAction {
    @Autowired
    private TestAnswerService testAnswerService;

    @ApiOperation("查询所有试卷答题表信息")
    @RequestMapping(value = "/selectAllTestAnswer",method = RequestMethod.GET)
    public ResponseMessage selectAllTestAnswer(){
        Map<String, List> map=new HashMap<>();
        List<Testanswer> list=testAnswerService.selectAllTestAnswer();
        if (!list.isEmpty()){
            map.put("TestAnswer",list);
            return Result.success("查询成功",map);
        }
        return Result.error("查询失败",false);
    }

    @ApiOperation("插入试卷答题表信息")
    @RequestMapping(value = "/insertTestAnswer",method = RequestMethod.GET)
    public ResponseMessage insertTestAnswer(Testanswer testanswer){
        int temp=0;
        if (testanswer!=null){

                temp=testAnswerService.insertTestanswer(testanswer);
                if (temp>0) {
                    return Result.success("插入试卷答题表信息成功",temp);
                }
                else  {
                    return Result.error("该试卷答题表已存在",temp);
                }

        }
        return Result.error("插入试卷答题表信息失败",temp);
    }

    @ApiOperation("删除试卷答题表信息")
    @RequestMapping(value = "/deleteTestAnswer",method = RequestMethod.GET)
    public ResponseMessage deleteTestAnswer(Testanswer testanswer){
        int temp=0;
        if (testanswer!=null){
            if (testanswer.getTestanswerid()!=0){
                temp=testAnswerService.deleteTestanswer(testanswer.getTestanswerid());
                if (temp>0) {return Result.success("删除试卷答题表信息成功",temp);}
                else {return Result.error("无该试卷答题表",temp);}
            }
        }
        return Result.error("删除试卷答题表信息失败",temp);
    }

    @ApiOperation("修改试卷答题表信息")
    @RequestMapping(value = "/updateTestanswer",method = RequestMethod.GET)
    public ResponseMessage updateTestanswer(Testanswer testanswer){
        int temp=0;
        if (testanswer!=null){
            if (testanswer.getTestanswerid()!=0){
                temp=testAnswerService.updateTestanswer(testanswer);
                if (temp>0) {
                    return Result.success("更新试卷答题表信息成功",temp);
                }
                else {
                    return Result.error("无该试卷答题表信息",temp);
                }
            }
        }
        return Result.error("更新试卷答题表信息失败",temp);
    }
}
